import Vue from 'vue'
import VueRouter from 'vue-router'
import { MessageBox } from 'element-ui'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: '登录页',
    component: () => import('_views/Login/index.vue')
  },
  {
    path: '/HomeView',
    name: '主页',
    component: () => import('_views/HomeView')
  },
  // {
  //   path: '/',
  //   name: '',
  //   component: () => import('_views/AdminView'),
  //   children: [
  //     {
  //       path: '/HomeView',
  //       name: '主页',
  //       component: () => import('_views/HomeView')
  //     }
  //   ]
  // },
  {
    path: '/DormManage',
    name: '宿舍管理',
    component: () => import('_views/AdminView'),
    children: [
      {
        path: '/AccommodationManage',
        name: '住宿管理',
        component: () => import('_views/DormManage/AccommodationManage')
      },
      {
        path: '/CampusManage',
        name: '校区管理',
        component: () => import('_views/DormManage/CampusManage')
      },
      {
        path: '/BuildingManage',
        name: '楼宇管理',
        component: () => import('_views/DormManage/BuildingManage')
      },
      {
        path: '/RoomManage',
        name: '房间管理',
        component: () => import('_views/DormManage/RoomManage')
      },
      {
        path: '/DepartmentManage',
        name: '部门管理',
        component: () => import('_views/DormManage/DepartmentManage')
      },
      {
        path: '/CollegeManage',
        name: '学院管理',
        component: () => import('_views/DormManage/CollegeManage')
      },
      {
        path: '/ApplyList',
        name: '申请列表',
        component: () => import('_views/DormManage/ApplyList')
      },
      {
        path: '/BedManage',
        name: '床位管理',
        component: () => import('_views/DormManage/BedManage')
      },
      {
        path: '/OldStudent',
        name: '老生续住管理',
        component: () => import('_views/DormManage/OldStudent')
      },
      {
        path: '/GraduateManage',
        name: '研究生新生入住管理',
        component: () => import('_views/DormManage/GraduateManage')
      },
      {
        path: '/ClassRoomManage',
        name: '班级管理',
        component: () => import('_views/DormManage/ClassRoomManage')
      },
      {
        path: '/SanitaryExamination',
        name: '卫生检查',
        component: () => import('_views/DormManage/SanitaryExamination')
      },
      {
        path: '/PersonExamination',
        name: '个人卫生检查',
        component: () => import('_views/DormManage/PersonExamination')
      },
      {
        path: '/PersonRule',
        name: '个人违纪检查',
        component: () => import('_views/DormManage/PersonRule')
      },
      {
        path: '/CheckRoom',
        name: '智能查寝',
        component: () => import('_views/DormManage/CheckRoom')
      },
    ]
  },
  {
    path: '/MaintainView',
    name: '报修管理',
    component: () => import('_views/AdminView'),
    children: [
      {
        path: '/MaintainForm',
        name: '报修单管理',
        component: () => import('_views/MaintainView/MaintainForm')
      },
      {
        path: '/MaintainMaterial',
        name: '报修材料使用记录',
        component: () => import('_views/MaintainView/MaintainMaterial')
      },
      {
        path: '/MaterialManage',
        name: '报修材料管理',
        component: () => import('_views/MaintainView/MaterialManage')
      },
      {
        path: '/MaintainType',
        name: '报修类型设置',
        component: () => import('_views/MaintainView/MaintainType')
      },
      {
        path: '/MaintainArea',
        name: '报修工区域设置',
        component: () => import('_views/MaintainView/MaintainArea')
      },
      {
        path: '/MaintainTime',
        name: '报修时间段设置',
        component: () => import('_views/MaintainView/MaintainTime')
      },
      {
        path: '/Evaluate',
        name: '评价管理',
        component: () => import('_views/MaintainView/Evaluate')
      }
    ]
  },
  {
    path: '/UserManage',
    name: '用户管理',
    component: () => import('_views/AdminView'),
    children: [
      {
        path: '/Maintainer',
        name: '维修工管理',
        component: () => import('_views/UserManage/Maintainer')
      },
      {
        path: '/SchoolWorker',
        name: '校工管理',
        component: () => import('_views/UserManage/SchoolWorker')
      },
      {
        path: '/Student',
        name: '学生管理',
        component: () => import('_views/UserManage/Student')
      }
    ]
  },
  {
    path: '/authority',
    name: '权限管理',
    component: () => import('_views/AdminView'),
    children: [
      {
        path: '/RoleManage',
        name: '角色管理',
        component: () => import('_views/RoleManage')
      }
    ]
  },
  ,
  {
    path: '/system',
    name: '系统管理',
    component: () => import('_views/AdminView'),
    children: [
      {
        path: '/MenuManage',
        name: '菜单管理',
        component: () => import('_views/MenuManage')
      }
    ]
  },
  {
    path: '/content',
    name: '内容管理',
    component: () => import('_views/AdminView'),
    children: [
      {
       path: '/Message',
       name: '通知公告',
       component: () => import('_views/Message')
      },
      {
       path: '/Advice',
       name: '意见建议',
       component: () => import('_views/Advice')
      },
      {
        path: '/AdviceType',
        name: '意见分类',
        component: () => import('_views/AdviceType')
      },
      {
        path: '/SatisfactionSurvey',
        name: '满意度调查',
        component: () => import('_views/SatisfactionSurvey')
       },
    ]
  },
  {
    path: '/FlowManage',
    name: '流程管理',
    component: () => import('_views/AdminView'),
    children: [
      {
       path: '/ModelManage',
       name: '模型管理',
       component: () => import('_views/FlowManage/ModelManage')
      },
      {
        path: '/flowManage',
        name: '流程管理',
        component: () => import('_views/FlowManage/flowManage')
      },
      {
        path: '/FastSay',
        name: '快捷短语管理',
        component: () => import('_views/FlowManage/FastSay')
       },
    ]
  },
  ,
  {
    path: '/MyBusiness',
    name: '我的事务',
    component: () => import('_views/AdminView'),
    children: [
      {
       path: '/startBusiness',
       name: '发起事务',
       component: () => import('_views/MyBusiness/startBusiness')
      },
      {
        path: '/claimBusiness',
        name: '待签事务',
        component: () => import('_views/MyBusiness/claimBusiness')
      },
      {
        path: '/todoBusiness',
        name: '待办事务',
        component: () => import('_views/MyBusiness/todoBusiness')
      },
      {
        path: '/sendBusiness',
        name: '已发事务',
        component: () => import('_views/MyBusiness/sendBusiness')
      },
      {
        path: '/doneBusiness',
        name: '办结事务',
        component: () => import('_views/MyBusiness/doneBusiness')
       },
    ]
  },
  {
    path: '/PalyEducation',
    name: '劳动教育',
    component: () => import('_views/AdminView'),
    children: [
      {
       path: '/courseManagement',
       name: '课程管理',
       component: () => import('_views/PalyEducation/courseManagement')
      },
      {
        path: '/studentManagement',
        name: '学生管理',
        component: () => import('_views/PalyEducation/studentManagement')
       },
    ]
  },
  {
    path: '/PatrolManage',
    name: '巡更管理',
    component: () => import('_views/AdminView'),
    children: [
      {
       path: '/PatrolPoint',
       name: '巡更点配置',
       component: () => import('_views/PatrolManage/PatrolPoint')
      },
      {
        path: '/PatrolPaly',
        name: '巡更计划',
        component: () => import('_views/PatrolManage/PatrolPaly')
      },
      {
        path: '/PatrolExcel',
        name: '巡更报表',
        component: () => import('_views/PatrolManage/PatrolExcel')
      },
    ]
  },
  {
    path: '/openManage',
    name: '开放时间管理',
    component: () => import('_views/AdminView'),
    children: [
      {
        path: '/openTime',
        name: '开放状态设置',
        component: () => import('_views/OpenManage')
      }
    ]
  },
]



const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})


router.beforeEach((to, from, next) => {
  // console.log(to.path);
  const isLogin = () => !!localStorage.getItem('token')
  const menus = JSON.parse(window.localStorage.getItem("menus"))
  const canToMenus=[]
  if (to.path !== '/login') {
    if (isLogin) {
      menus.forEach(Item => {
        Item.children.forEach(item => {
          canToMenus.push(item.path)
        })
      })
      canToMenus.push('/HomeView')
      // console.log(canToMenus);
      if (canToMenus.includes(to.path)) {
        next()
      } else {
        MessageBox.confirm('无权限访问！', '提示', {
          confirmButtonText: '确定',
          showCancelButton:false,
          type: 'warning'
        }).then(() => {
          router.go(-1)
        })
      }
    } else {
      next('/login')
    }
  } else {
    next()
  }
})

export default router

